// ThreadResult.cpp: implementation of the CThreadAssociation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Rock.h"
#include "ThreadAssociation.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CThreadAssociation::CThreadAssociation()
{

}

CThreadAssociation::~CThreadAssociation()
{

}
	
void CThreadAssociation::AppendText(const char* Text){
	m_OffScreenStrList.push_back(Text);
	if(m_OffScreenStrList.size() ==10000){
		int len = 5000;
		while(len-->0)m_OffScreenStrList.pop_front();
	}
	if(GetHwnd() == NULL)return;

	PARAFORMAT      pf;
    pf.cbSize = sizeof(PARAFORMAT);
	pf.dwMask = PFM_OFFSET|PFM_STARTINDENT;
	pf.dxOffset  = 100;
	pf.dxStartIndent = 100;
	::SendMessage(m_hEdit,EM_SETPARAFORMAT,0,(LPARAM)&pf);

	int nFirstLine = (int)::SendMessage( m_hEdit, EM_GETFIRSTVISIBLELINE, 0, 0 );
	::SendMessage(m_hEdit,EM_HIDESELECTION, TRUE,TRUE);
	int	 len = GetWindowTextLength(m_hEdit);

	int n = m_OffScreenStrList.size();
	for(int i=0; i<n;i++){
			
		CHARRANGE cr;
		cr.cpMax = len;
		cr.cpMin = len;
		::SendMessage(m_hEdit,EM_EXSETSEL, 0, (LPARAM)&cr);
		
		::SendMessage(m_hEdit,EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&m_cfText);
		::SendMessage(m_hEdit,EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)Text); 
		::SendMessage(m_hEdit,EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)"\r\n"); 
		
		cr.cpMax = -1;
		cr.cpMin = -1;
		::SendMessage(m_hEdit,EM_EXSETSEL, 0, (LPARAM)&cr);
		
		if (len > 12500)
		{
			cr.cpMax = 0;
			cr.cpMin = 5000;
			::SendMessage(m_hEdit,EM_EXSETSEL, 0, (LPARAM)&cr);
			
			string s;
			::SendMessage(m_hEdit,EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)s.c_str());
			
			cr.cpMax = len;
			cr.cpMin = len;
			::SendMessage(m_hEdit,EM_EXSETSEL, 0, (LPARAM)&cr);
		}
		
	}
	m_OffScreenStrList.clear();

	::SendMessage(m_hEdit,EM_HIDESELECTION, FALSE,TRUE);
	int	nFirstLineNow = (int)::SendMessage( m_hEdit, EM_GETFIRSTVISIBLELINE, 0, 0 );
	::SendMessage( m_hEdit, EM_LINESCROLL, 0, (LPARAM) nFirstLine - nFirstLineNow);

}
